package com.theme.passport.controller.theme;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.view.R;
import com.theme.passport.controller.sys.AbstractController;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.constant.UserTypeEnum;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.service.SysUserService;
import com.theme.passport.service.theme.ThemeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 设计师信息
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-07-10 14:30:04
 */
@RestController
@RequestMapping("theme/designer-info")
public class DesignerInfoController extends AbstractController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ThemeService themeService;

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("theme:designer-info:info")
    public R info(){
        Long userId = getUserId();
        SysUserEntity designer = sysUserService.getById(userId);
        if (!designer.getUserType().equals((byte) UserTypeEnum.DESIGNER.getValue())){
            return R.ok().put("designer", designer);
        }
        QueryWrapper<ThemeEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper.eq("user_id", userId));
        List<ThemeEntity> list = themeService.list(orderWrapper);
        designer.setThemeList(list);
        return R.ok().put("designer", designer);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("theme:designer-info:update")
    public R update(@RequestBody SysUserEntity themeDealer){
        sysUserService.updateById(themeDealer);
        return R.ok();
    }

}
